package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.comment.response.JsonResult;
import cn.tedu._05weibo.comment.response.StatusCode;
import cn.tedu._05weibo.mapper.CommentMapper;
import cn.tedu._05weibo.pojo.dto.CommentDTO;
import cn.tedu._05weibo.pojo.entity.Comment;
import cn.tedu._05weibo.pojo.vo.CommentVO;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Api(tags = "03.评论管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 8.发布评论功能
     */
    @ApiOperation(value ="8.发布评论功能")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        log.debug("commentDTO = " + commentDTO);
        UserVO userVO = (UserVO) session.getAttribute("user");
        // 判断用户是否登录
        if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);// 未登录
        }
        // 封装数据
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMapper.insert(comment);
        // 成功
        return new JsonResult(StatusCode.LOGIN_SUCCESS);
    }

    /**
     * 9.获取评论功能
     */
    @ApiOperation(value = "9.获取评论功能")
    @GetMapping("selectByWeiboId")
    @ApiImplicitParam(name = "id", value = "微博编号",required = true,dataType = "int")
    public JsonResult selectByWeiboId(Integer id) {
        log.debug("id = " + id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS, commentMapper.selectByWeiboId(id));
    }

}
